package testsystem;

/*import com.romaremez.testuser.src.questionanswer.Question;
import com.romaremez.testuser.src.userdata.UserSession;
import com.romaremez.testuser.src.datautils.DataLoader;   */

import datautils.DataLoader;
import datautils.XMLQuestionSource;
import datautils.XMLQuestionSourceXPath;
import questionanswer.Question;
import userdata.UserSession;

import java.util.Collections;
import java.util.List;
import java.util.Scanner;

/**
 * Класс управляет действиями юзера
 */
public class TestUser {
    private UserSession userSession;
    private List<Question> questions;//, questions1;

    public TestUser(UserSession userSession) {
        this.userSession = userSession;
        getQuestions();
    }

    /**
     * Method for read and shuffle questions.
     */
    private void getQuestions() {
        questions = new XMLQuestionSourceXPath().getQuestions();
        /*new DataLoader().readQuestionList();
        questions1 = new DataLoader().readQuestionList1();
        Collections.shuffle(questions);
        Collections.shuffle(questions1);*/
    }

    public void startTest() {
        askQuestions();
        if (isTestComplete()) {
            endTest();
            userSession.finishSession();
        }
    }

    private boolean isTestComplete() {
        return userSession.size() >= questions.size();
    }

    /**
     * Method looks for next questionStep.
     */
    private QuestionStep getNextQuestionStep() {
        return chooseArray(questions);
       /*if (userSession.size()<5){
            return  chooseArray(questions);
       } else if ((userSession.size()>=5)&&(userSession.size()<10)){
            return chooseArray(questions1);
       } else if ((userSession.size()>=10)){
           switch (userSession.getWorseUsersKnownTheme()){
               case "основы ООП":
                   return chooseArray(questions);
               case "Общеобразовательное":
                   return chooseArray(questions1);
           }
       }
        return null;*/
    }

    private QuestionStep chooseArray (List<Question> questions) {
        QuestionStep result;
        for (Question question : questions) {
            result = new QuestionStep(question);
            if ((!userSession.contains(result))) {
                userSession.addQuestionStep(result);
                return result;
            }
        }
        return null;
    }

//    private QuestionStep firstStage (QuestionStep result){
//        if ((!userSession.contains(result))&&) {
//            userSession.addQuestionStep(result);
//            return result;
//        }
//    }

    private QuestionStep getPreviousStep() {
        int previousIndex = userSession.size() - 1;

        if ( previousIndex < 0) {
            return getNextQuestionStep();
        }
       return userSession.getQuestionStep(previousIndex);
    }

    private void deleteCurrentQuestionStep(QuestionStep questionStep) {
        userSession.deleteQuestionStep(questionStep);
    }

    private void askQuestions() {
        if (userSession.size()<questions.size())
        System.out.println("Вопрос № "+(userSession.size()+1));
        QuestionStep currentStep = getNextQuestionStep();
        System.out.println("Ваши текущие результаты ");
        userSession.printResults();
        while (true) {
            if (currentStep == null) {
                break;
            }
            currentStep.printQuestion();
            String userInput = getAnswer();
            switch (userInput) {
                case "<undo>":
                            deleteCurrentQuestionStep(currentStep);
                            System.out.println("Вопрос № "+(userSession.size()));
                            currentStep = getPreviousStep();
                            System.out.println("Ваши текущие результаты ");
                            userSession.printResults();
                            break;

                case "<exit>":
                            deleteCurrentQuestionStep(currentStep);
                            return;
                default:
                            currentStep.saveAnswer(userInput);
                            if (userSession.size()<questions.size())
                            System.out.println("Вопрос № "+(userSession.size()+1));
                            currentStep = getNextQuestionStep();
                            System.out.println("Ваши текущие результаты ");
                            userSession.printResults();
            }
        }
    }

    /**
     * Method reads the answer from console.
     */
    private String getAnswer() {
        Scanner in = new Scanner(System.in);
        System.out.println("Введите ответ:");
        return in.nextLine();
    }

    /**
     * Method which ends the test by printing the user's score.
     */
    private void endTest() {
        System.out.printf("Тест окончен");
    }
}



